﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using LiuYan.Entity;
using LiuYan.Service;

namespace LiuYan.Web.Areas.Admin.Controllers
{
    public class GoodsController : BaseController
    {
        //
        // GET: /Admin/Goods/

        public ActionResult Index()
        {
            ViewData["sort"] = new SortService().GetList();
            return View();
        }
        public ActionResult Window( int id)
        {
            ViewData["id"] = id;
            ViewData["sort"] = new SortService().GetList();
            ViewData["att"] = new GoodService().GetAttribute(id);
            return View();
        }
        public ActionResult PingJia(int gid)
        {
            ViewData["gid"] = gid;
            return View();
        }

        /// <summary>
        /// 热销商品
        /// </summary>
        /// <returns></returns>
        public ActionResult RxGood()
        {

            return View();
        }
       
        public ActionResult AddActivityGood(string ope)
        {
            ViewData["sort"] = new SortService().GetList();
            ViewData["ope"] = ope;
            return View();
        }

        public string GetGoodInfo(int sid)
        {
            return JsonConvert.SerializeObject(new GoodService().GetGoodT(sid));
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pageCount"></param>
        /// <param name="pageIndex"></param>
        /// <param name="name"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public string GetList(int pageCount, int pageIndex, string name, int sort)
        {
            return JsonConvert.SerializeObject(new GoodService().GetList(pageCount, pageIndex, name, sort));
        }
        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="pageCount"></param>
        /// <param name="pageIndex"></param>
        /// <param name="name"></param>
        /// <param name="sort"></param>
        /// <returns></returns>
        public string GetPjList(int pageCount, int pageIndex, int gid)
        {
            return JsonConvert.SerializeObject(new GoodService().PingJia(pageCount, pageIndex, gid));
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <returns></returns>
        public string UploadImg()
        {
            HttpFileCollection file = System.Web.HttpContext.Current.Request.Files;
            int num = file.Count;
            string imgPath = "";
            TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string fileName = file[0].FileName.Split('\\').Last();
            imgPath = "/Content/goodimg/" + Convert.ToInt64(ts.TotalSeconds) + "." + fileName.Split('.').Last();
            string path = Server.MapPath(imgPath);
            file[0].SaveAs(path);
            return "{'status':'1','path':'" + imgPath + "'}";
        }

       /// <summary>
       /// 编辑商品
       /// </summary>
       /// <param name="form"></param>
       /// <returns></returns>
        public int Edit(FormCollection form)
        {
          
           TB_Goods entity=new TB_Goods();
            entity.Id = Convert.ToInt32(form["id"]);
            entity.Name = form["name"];
            entity.Price = Convert.ToDecimal(form["price"]);
            entity.DiscountPrice = Convert.ToDecimal(form["dprice"]);
            entity.Count = Convert.ToInt32(form["count"]);
            entity.PayCount = Convert.ToInt32(form["paycount"]);
            entity.Freight = Convert.ToDecimal(form["freight"]);
            entity.Xj = Convert.ToInt32(form["xj"]);
            var rx_good = Convert.ToInt32(form["rx_good"]);
           var attr = form["attlist"];
           List<TB_Good_Property> attlist = new List<TB_Good_Property>();
           if (attr != "")
           {
               var arr = attr.Split('◎');
               for (int i = 0; i < arr.Length; i++)
               {
                   var atttemp = arr[i].Split('^');
                   TB_Good_Property attentity = new TB_Good_Property();
                   attentity.Id = Convert.ToInt32(atttemp[0]);
                   attentity.Name = atttemp[1];
                   attentity.Value = atttemp[2];
                   attentity.Price = Convert.ToDecimal(atttemp[3]);
                   attlist.Add(attentity);
               }
           }
           else
           {

           }

           var imgs = form["imgs"].Split(',');
            var pl = form["pl"].Split(',');
           List<string> list=new List<string>();
            list = pl.ToList();
            list = list.OrderBy(a => int.Parse(a)).ToList();
            var temp = new int[5];
            for (int i = 0; i < list.Count; i++)
            {
                for (int j = 0; j < pl.Length; j++)
                {
                    if (pl[j] == list[i])
                    {
                        temp[i] = j;
                    }
                }
            }
            entity.Img1 = imgs.Length >= 1 ? imgs[temp[0]] : " ";
            entity.Img2 = imgs.Length >= 2 ? imgs[temp[1]] : " ";
            entity.Img3 = imgs.Length >= 3 ? imgs[temp[2]] : " ";
            entity.Img4 = imgs.Length >= 4 ? imgs[temp[3]] : " ";
            entity.Img5 = imgs.Length >= 5 ? imgs[temp[4]] : " ";
            entity.Des = Server.UrlDecode(form["des"]);
            entity.Date = DateTime.Now;
            entity.IsShow = Convert.ToInt32(form["show"]);
            entity.Sort = Convert.ToInt32(form["sort"]);
            entity.PayCountTrue = 0;

            var row = new GoodService().Edit(entity);
            if (row>0)
            {
                var id = 0;
                if (entity.Id > 0)
                {
                    id = entity.Id;
                }
                else
                {
                    id = row;
                }
                new GoodService().EditAttribute(attlist, id);

                if (rx_good == 0)
                {
                    new GoodService().DelRx(id);
                }
                else
                {
                    new GoodService().EditRxXp(id);
                }
               
            }
           return row;
        }

        /// <summary>
        /// 获取一条数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public string GetOneData(int id)
        {
            return JsonConvert.SerializeObject(new GoodService().GetOneGoodData(id));
            //return JsonConvert.SerializeObject(new BaseService().GetOneData(id, "TB_Goods"));
        }


        /// <summary>
        /// 获取热销列表
        /// </summary>
        /// <param name="pageCount"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public string GetRxList(int pageIndex, int pageCount, string name)
        {
            return JsonConvert.SerializeObject(new GoodService().GetRxGood(pageCount, pageIndex,name));
        }

        /// <summary>
        /// 获取新品列表
        /// </summary>
        /// <param name="pageCount"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        public string GetXpList(int pageIndex, int pageCount, string name)
        {
            return JsonConvert.SerializeObject(new GoodService().GetXpGood(pageCount, pageIndex,name));
        }

        /// <summary>
        /// 删除热销商品
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelRx(string ids)
        {
            return new BaseService().Dels(ids, "TB_RxGood", true);
        }
         /// <summary>
        /// 删除新品
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelXp(string ids)
        {
            return new BaseService().Dels(ids, "TB_XpGood", true);
        }
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelSp(string ids)
        {
            return new BaseService().Dels(ids, "TB_Goods", false);
        }
        /// <summary>
        /// 删除商品
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public int DelPj(string ids)
        {
            return new BaseService().Dels(ids, "TB_Evaluate", false);
        }
        public int EditXpRx(int gid)
        {
            return new GoodService().EditRxXp(gid);

        }
    }
}
 

